// pages/video/video.js
const app = getApp()
const util = require('../../utils/util.js')
const clock = require('../../utils/clock.js')
var animation = wx.createAnimation({
  duration: 200,
  timingFunction: "ease",
})
Page({
  data: {
    vid: '', //vid
    spid: '', //视频对应id
    sptitle: '', //标题
    fenxiang: "", //分享标题
    imgUrl: "", //封面图
    cimg: "", //分享图片
          jiazai: false, //加载
         channel: "",//渠道
          back:true,//是否返回按钮
    // count: 0, // 设置 计数器 初始为0
        watchDuration:"",//播放次数
          totalMinutes:"",//总时间
    countTimer: null, // 设置 定时器 初始为null
    show: false,
    inHide: true,
share_user: "",//分享用户

  },
  countInterval: function() {
    // 设置倒计时 定时器 每1000毫秒执行一次
          var that = this
          this.countTimer = setInterval(() => {
                  //     console.log(app.globalData.vdCount)

                  if (app.globalData.vdCount < 50) {
                          animation.translateY(0).step();
                          that.setData({
                                  animationData: animation.export(),
                                  show: false
                          })
                  } else {
                          animation.translateY(-62).step();
                          that.setData({
                                  animationData: animation.export(),
                                  show: true
                          })
                  }
          }, 200) 
  
  },
        // 视频返回的播放次数
        muindata:function(res){
                console.log(res)
                if (res.data.minutes > 9 || res.data.totalMinutes >60){
                        this.setData({
                                inHide: true
                        })
                }
        },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(this.countTimer);
  },
       
  bindtimeupdate: function(e) {
          if (!this.data.inHide) {
                
                  if (app.globalData.vdCount >51) {
                          app.globalData.vdCount =0

                          var header = {
                                  'authorization': wx.getStorageSync('tekon'),
                                  'content-type': "application/x-www-form-urlencoded"
                          }
                          var data = {
                                  minutes: 1,
                                  videoId: this.data.spid
                          }
                          util.formhttp(app.globalData.appurl + '/mission/watchTime', 'post', data, header, this.muindata);
                  } else {
                          app.globalData.vdCount = app.globalData.vdCount + 0.42
                  }
                  this.setData({
                          counter: app.globalData.vdCount
                  })
          }
  },
statdata:function(res){
        console.log(res)
        var resdata = res.data.userMissions.commonMissions
        for (var i=0 ;i<resdata.length; i++){
                if (res.data.userMissions.commonMissions[i].id==11){
                        this.setData({ mux: res.data.userMissions.commonMissions[i].reward })
                        // console.log(res.data.userMissions.commonMissions[i].reward)
                }
        }
},

// 添加客服
kefu:function(){
        this.setData({
                kefushow:true
        })
        },
  onLoad: function(options) {
       
    var that = this
          that.setData({
                  spid: options.id,
                  headerheight: app.globalData.navHeight,
                  cimg: "https://img-1256481299.cos.ap-guangzhou.myqcloud.com/xqimg/" + options.id + "_share_croped.jpg",
          })
          // 判断系统

          if (wx.getSystemInfoSync().platform == 'ios') {
                  that.setData({
                          toptip: false,
                           rate:1.47
                  })
          } else if (wx.getSystemInfoSync().platform == 'android'){
            that.setData({
              toptip: true,
              rate: 3.75
            })
          } else {
                  that.setData({
                          toptip: true,
                    rate: 3.0
                  })
          }
        //   this.chakan()
          this.request()
          this.reward()
          this.jinbis()

    this.countInterval(); 
          // 判断入口
          if (!(options.user_id == undefined)) {
                  that.setData({
                          share_user: options.user_id,
                          back:false
                  })
          }  
  },

jinbis:function(){
        var header = {
                'authorization': wx.getStorageSync('tekon'),
                'content-type': "application/x-www-form-urlencoded"
        }
        util.formhttp(app.globalData.appurl+'/mission', 'get', '', header, this.statdata);
},
        // 获取打赏金额
        reward: function () {
                var header = {
                        'authorization': wx.getStorageSync('tekon'),
                        'content-type': "application/x-www-form-urlencoded"
                }   
                util.formhttp(app.globalData.appurl +'/reward', 'GET', "", header, this.rewardData);
        },
        rewardData: function (res){
                console.log(res)
                this.setData({
                        nums: res.data
                })
        },
        // 登录
        login: function () {
                var that = this
                wx.login({
                        success: function (res) {
                                var code = res.code
                                var data = {
                                        userCode: code,
                                        channel: that.data.channel,
                                        userImageUrl: "",
                                        userName: "",
                                        promoterId: that.data.share_user
                                }
                                util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        succeed: function (res) {
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
                this.request()

        },

        

  onShareAppMessage: function(res) {
    var that = this
          const userid = wx.getStorageSync('userInfo').id
    var imgUrl = that.data.imgUrl
    if (imgUrl.indexOf("myqcloud.com") > 0) {
      if (res.from === 'button') {
        // 来自页面内转发按钮
      }
      return {
        title: that.data.fenxiang[Math.floor(Math.random() * that.data.fenxiang.length)],
        path: '/pages/video/video?id=' + that.data.spid + '&user_id=' + userid,
        imageUrl: that.data.cimg,
      }
    } else {
      if (res.from === 'button') {
        // 来自页面内转发按钮
      }
      return {
        title: that.data.fenxiang[Math.floor(Math.random() * that.data.fenxiang.length)],
        path: '/pages/video/video?id=' + that.data.spid + '&user_id=' + userid,
      }

    }
  },
  name: function() {
    var that = this
    var title = that.data.sptitle
    console.log(title)
    var name = [];
    var name1 = "超级好听的" + title + "，根本停不下来！";
    var name2 = "快来和我一起听" + title + "，从没见过这么好听的！";
    var name3 = "这首" + title + "听哭所有人，每天都要听一遍";
    var name4 = "只分享给和我关系最好的人，这首" + title + "一定要收藏！";
    name[0] = name1
    name[1] = name2
    name[2] = name3
    name[3] = name4
    // console.log(name)
    that.setData({
      fenxiang: name
    })

  },
  // 返回首页
  backindex: function() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
// 前往收益
        earnings: function () {
                wx.navigateTo({
                        url: '/pages/earnings/earnings',
                })
        },
        
  // 上报看过的视频
  chakan: function() {
    var that = this
    var data = {
        userId: wx.getStorageSync('userInfo').id,
        videoId: that.data.spid
    };
          var header = {
                  'authorization': wx.getStorageSync('tekon'),
                  'content-type': "application/x-www-form-urlencoded"
          }
        util.formhttp(app.globalData.appurl +'/qinqiang/watch', 'POST', data, header,null);

  },
  // 点击列表播放
  bofang: function(e) {
    var that = this
    that.setData({
      spid: e.currentTarget.dataset.id,
      cimg: "https://img-1256481299.cos.ap-guangzhou.myqcloud.com/xqimg/" + e.currentTarget.dataset.id + "_share_croped.jpg",
    })
    that.request()
//     that.chakan()
    that.name()
  },
// 重新加载
jiazai: function () {
        this.request()
},
  // 请求信息
  request: function() {
    var that = this
    var spid = that.data.spid
    wx.showLoading({
      title: '加载中...',
    })
    var data = {
            userId: wx.getStorageSync('userInfo').id
    };
          var header = {
                  'authorization': wx.getStorageSync('tekon'),
                  'content-type': "application/x-www-form-urlencoded"
          }
          
        util.formhttp(app.globalData.appurl +'/qinqiang/video/'+ spid, 'GET', data, header, that.porcessData);
  },
  porcessData: function(res) {
          console.log(res)
          this.reward()
          this.jinbis()
          if (res.code == 1) {
                  this.setData({
                          jiazai: true
                  })
          } else {
                  this.setData({
                          jiazai: false
                  })
          }
          if (res.status == 401) {
                  this.login()
          }
    wx: wx.hideLoading()
    var vidinfo = res.data.video
    // 播放信息
    this.setData({
      vid: vidinfo.videoUrl.match(/page\/(\S*).html/)[1],
      imgUrl: vidinfo.imgUrl,
      sptitle: vidinfo.title,
      hits: vidinfo.hits,
      isCollection: vidinfo.favorTime,
        watchDuration: vidinfo.watchDuration,
        totalMinutes: res.data.totalMinutes
    })
// 判断播放次数
          console.log(vidinfo.watchDuration )
          if (vidinfo.watchDuration > 9 || res.data.totalMinutes> 60) {
                this.setData({
                        inHide: true
                })
        }else{
                  this.setData({
                          inHide: false
                  })
        }
    // 标题
    wx.setNavigationBarTitle({
      title: vidinfo.title,
    })
    var list = [];
    var reslist = res.data.relatedVideos
    for (var ids in reslist) {
      var detail = reslist[ids];
      var temp = {
        id: detail.id,
        title: detail.title,
        imgUrl: detail.imgUrl,
      }
      list.push(temp)
    }
    this.setData({
      correlation_list: list
    })
    this.name()
  }

})